<?php
header('Content-Type: application/json; charset=utf-8');

// 引入数据库配置
$dbConfig = include('db_config.php');

// 获取前端传过来的文章 ID
$article_id = $_POST['article_id'] ?? '';

// 检查文章 ID 是否为空
if (empty($article_id)) {
    $result = [
        'status' => 'error',
        'message' => '文章 ID 不能为空'
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
    exit;
}

// 创建数据库连接
try {
    $dsn = "mysql:host=" . $dbConfig['host'] . ";dbname=" . $dbConfig['dbname'] . ";charset=" . $dbConfig['charset'];
    $pdo = new PDO($dsn, $dbConfig['username'], $dbConfig['password']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 删除文章
    $deleteArticleStmt = $pdo->prepare("DELETE FROM mi_bbs WHERE id = :article_id");
    $deleteArticleStmt->bindParam(':article_id', $article_id);
    $deleteArticleStmt->execute();

    // 检查删除是否成功
    if ($deleteArticleStmt->rowCount() > 0) {
        $result = [
            'status' => 'success',
            'message' => '文章删除成功'
        ];
    } else {
        $result = [
            'status' => 'error',
            'message' => '文章删除失败或文章不存在'
        ];
    }

    echo json_encode($result, JSON_UNESCAPED_UNICODE);

} catch (PDOException $e) {
    // 数据库操作失败
    $result = [
        'status' => 'error',
        'message' => '数据库操作失败：' . $e->getMessage()
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
}
?>